package com.zy.service.bs.impl;

import com.zy.dao.bs.IntegralDao;
import com.zy.dao.bs.IntegralDetailDao;
import com.zy.entity.bs.Integral;
import com.zy.entity.bs.IntegralDetail;
import com.zy.pojo.constants.Constant;
import com.zy.pojo.exceptions.ServiceException;
import com.zy.utils.NumberUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zy.dao.bs.SignDao;
import com.zy.entity.enums.StatusEnum;
import com.zy.entity.bs.Sign;
import com.zy.pojo.query.bs.SignQuery;
import com.zy.pojo.vos.common.PageVo;
import com.zy.service.base.impl.BaseServiceImpl;
import com.zy.service.bs.SignService;
import com.zy.utils.DateUtil;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Objects;

@Service("signService")
public class SignServiceImpl extends BaseServiceImpl<Sign> implements SignService {

	@Autowired
	private SignDao signDao;
	@Autowired
	private IntegralDao integralDao;
	@Autowired
	private IntegralDetailDao integralDetailDao;

	@Override
	public PageVo pageSign(SignQuery signQuery, Integer start, Integer limit) {
		return new PageVo(start, limit, this.signDao.countSign(signQuery), this.signDao.listSign(signQuery, start, limit));
	}

	@Override
	public void saveSign(Sign sign) {
		long time = DateUtil.getTime();

		this.signDao.save(sign);
	}
	
	@Override
	public void updateSign(Sign sign) {

		this.signDao.updateByPrimaryKeySelective(sign);
	}

	@Override
	public void removeSign(long id) {
		try {
			this.signDao.removeByPrimaryKey(id);
		} catch (Exception e) {
			log.info(e.getMessage(),e);
			Sign sign = new Sign();
			sign.setId(id);

			this.signDao.updateByPrimaryKeySelective(sign);
		}
	}

	@Override
	public void recover(long id) {
		Sign sign = new Sign();
		sign.setId(id);
		this.signDao.updateByPrimaryKeySelective(sign);
	}

	@Override
	public void sign(Long userId) {
		Example signEx = new Example(Sign.class);
		signEx.createCriteria().andEqualTo("userId",userId).andEqualTo("signTime", DateUtil.getDateTime());
		Sign sign = signDao.getByExample(signEx);
		if(Objects.nonNull(sign)){
			throw new ServiceException("您今天已签到!请不要重复签到");
		}
		Example ex  = new Example(Integral.class);
		ex.createCriteria().andEqualTo("userId",userId);
		Integral integral = integralDao.getByExample(ex);
		if(Objects.isNull(integral)){
			integral = new Integral();
			integral.setUserId(userId);
			integral.setIntegral(Constant.INTERGAL);
			integralDao.save(integral);
		}else{
			integral.setIntegral(NumberUtil.add(integral.getIntegral(),Constant.INTERGAL));
			integralDao.updateByPrimaryKeySelective(integral);
		}
		IntegralDetail integralDetail  = new IntegralDetail();
		integralDetail.setDescription("每日签到增加1积分");
		integralDetail.setFlow(1);
		integralDetail.setOpeationTime(DateUtil.getTime());
		integralDetail.setIntegralId(integral.getId());
		integralDetail.setIntegral(Constant.INTERGAL);
		integralDetailDao.save(integralDetail);
		sign = new Sign();
		sign.setUserId(userId);
		sign.setSignTime(DateUtil.getDateTime());
		signDao.save(sign);

	}

}
